package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.data.RoleVo;
	import engine.event.RoleEffectEvent;
	import engine.event.SceneEvent;
	import engine.event.SoundEvent;
	import engine.flag.ResourcesPath;
	import engine.logic.LogSendLogic;
	import engine.logic.event.LogicEvent;
	import engine.logic.walk.WalkManager;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	
	public class TopPlayerLogic
	{
		public var _ui		:	TopPlayerUI;
		
		/**玩家名*/
		private var _nameText	:	TextField;
		/**等级*/
		private var _lvText	:	TextField;
		/**元宝*/
		private var _rmbText	:	TextField;
		/**vip*/
		private var _vipText	:	TextField;
		/**金币*/
		private var _goldText	:	TextField;
		/**血量*/
		private var _hpText	:	TextField;
		/**体力*/
		private var _spText	:	TextField;
		
		private var _hpLine	:	MovieClip;
		
		private var _spLine	:	MovieClip;
		
		private var _vipMc		:	MovieClip;
		
		
		private var _facePlace	:	MovieClip;
		private var _recharge	:	SimpleButton;
		
		private var _vip_btn	: 	SimpleButton;
		private var _bang_btn	: 	SimpleButton;
		private var _vip_1		:	MovieClip ;
		
		private var _faceClip	:	NxClip;
		
		private var _hpLineWidth:	int;
		
		private var _spLineWidth:	int;
		
		private var _model		:	ModelLocator;
		
		private var _role		:	RoleVo;
		
		private var _bindManage:	BindManage;
		
		private static var _self:	TopPlayerLogic;
		
		//sp 加成
		private  var _spBtn:SimpleButton ; 
		
		//当前购买体力的次数
		public var _spCount:int ; 
		// 你可以购买几次 体力
		public var _addSpCount:uint ; 
		//VIP 当前等级
		private var _vipLevel:int ; 
		//init
		private var _strObj:Object;
		//记录次数
		private  var _ClickAlwa:uint = 0 ; 
		// 超出SP 最大的值
		private  var _beyondSPMax:int ; 
		/**BUFF图标界面***/
		private var _hortation:HortationUI;
		//够买 SP  条件限制
		private var _roleLevel:int ; 
		
		private var _level		:int;
		//
		private var _magicCoin:int ; 
		
		private var fastTrack:FastTrackUI
		/**
		 *缓存竞技场排名 
		 */		
		private var _rank:int	=	-1;
		private var _bangMobileAfterNumber:int = -1;       //手机验证
		private var bangMobileNumber:int = -1;       //手机验证
		private var acceddId:int;
		private var isWaitLevelUp:Boolean	=	false;
		public static function getInstance():TopPlayerLogic
		{
			if(_self == null)
			{
				_self	=	new TopPlayerLogic();
			}
			return _self;
		}
		public function init():void
		{
			_model		= ModelLocator.getInstance();
			if(!SceneManager._itemIconLoad)
			{
				_model.addEventListener(LogicEvent.ITEMICONLOADED,function():void
				{
					TopPlayerLogic.getInstance().playHeadPhoto = 1;
					_model.removeEventListener(LogicEvent.ITEMICONLOADED,arguments.callee);
				});
				
			}
			fastTrack	=	FastTrackUI.getInstance();
			
			fastTrack.y					=	120;
			
			
			_role		= _model.scene.Role;
			
			_bindManage	= BindManage.getInstance();
			
			_ui			= TopPlayerUI.getInstance();
			
			_nameText	= _ui.topPlayerBg.getChildByName( "nameText" ) as TextField;
			
			_lvText		= _ui.topPlayerBg.getChildByName( "lvText" ) as TextField;
			
			_rmbText	= _ui.topPlayerBg.getChildByName( "rmbText" ) as TextField;
			
			_goldText	= _ui.topPlayerBg.getChildByName( "goldText" ) as TextField;
			
			_vipText	= _ui.topPlayerBg.getChildByName( "vipText" ) as TextField;
			
			_hpText		= _ui.topPlayerBg.getChildByName( "hpText" ) as TextField;
			
			_spText		= _ui.topPlayerBg.getChildByName( "spText" ) as TextField;
			
			_hpLine		= _ui.topPlayerBg.getChildByName( "hpLine" ) as MovieClip;
			
			_spLine		= _ui.topPlayerBg.getChildByName( "spLine" ) as MovieClip;
			
			_recharge	= _ui.topPlayerBg.getChildByName( "AFK_btn" ) as SimpleButton;
			_vip_btn	= _ui.topPlayerBg.getChildByName( "VIP_btn" ) as SimpleButton;
			_bang_btn	= _ui.topPlayerBg.getChildByName( "bang" ) as SimpleButton;
			
			//
			_vip_1		=_ui.topPlayerBg.getChildByName( "vip" ) as MovieClip;
			
			
			_vipMc		= _ui.topPlayerBg.getChildByName( "vip" ) as MovieClip;			
			
			_spBtn 		=_ui.topPlayerBg.getChildByName("jiahao") as SimpleButton;
			
			//			_recharge.buttonMode = true;
			//_vip_1.gotoAndStop(0);
			_facePlace	= _ui.topPlayerBg.getChildByName( "face" ) as MovieClip;
			
			_faceClip	= new NxClip();
			_faceClip.isCache = true;
			_facePlace.addChild( _faceClip );
			
			_ui.addChild(fastTrack);
			
			//			_facePlace.mouseChildren = _facePlace.mouseEnabled = false;
			
			_hpLineWidth= _hpLine.width;
			
			_spLineWidth= _spLine.width;
			
			_model.addEventListener( SceneEvent.SceneLoaded	,onSceneLoaded );
			///添加BUFF界面-------------------------
			_hortation		=	new HortationUI();
			_hortation.x	=	100;
			_hortation.y	=	100;
			_ui.topPlayerBg.addChild(_hortation);
			
			_hpText.text = "";
			_spText.text = "";	
			
			_vip_1.buttonMode =true ; 
			_vip_btn.visible =true ; 
			
			_strObj=StaticIni.getIniObj("SP_Buy");
			_roleLevel=int(_strObj["roleLevel"]);
			
			//竞技场排名
			_model.addEventListener( String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_CLONE_PLAYER_CHALLENGE_TOP), getTop );
		}
		private function getTop(e:CustomMsgEvent):void
		{
			var name:String	=	e.pList[0];
			var rank:int	=	e.pList[1];
			if(name	!=	_role.getPlayerName()) return ;
			_rank=rank;
			showheadtips();
		}
		private function showheadtips():void
		{
			//			var _priority:int;
			//			_priority	=	EmbattleLogic.getInstance().proirityCount;
			var tip:String	=	(StaticIni.getSwfLangStrVar("UIString_1000080",[_rank,_role.getPlayerGuild()||StaticIni.getSwfLangStr("Common_Null"),_role.getPlayerFightPower()
				,_role.GetPropertiesEx(ActorPropertyUI.REPUTATION),_role.getSpeedRate()]));
			TipsMainLogic.instance.showString( tip );
		}
		private function requestForRank():void
		{
			SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_GET_CLONE_PLAYER_CHALLENGE_TOP, new WString(_role.getPlayerName()) );
		}
		private function onSceneLoaded(e:SceneEvent):void
		{
			_model.removeEventListener(SceneEvent.SceneLoaded	, onSceneLoaded );
			
			_nameText.text 	= _role.getPlayerName();
			
			_faceClip.source =  ResManager.newResClass( ResourcesPath.getHeadPic( _role.ModelId, _role.clotheModel )+".swf");
			
			bind();
		}
		
		private function bind():void
		{
			_level = _role.getPlayerLevel();
			
			var bindList : Array = [];
			//绑定等级
			bindList.push({propName:"Level"				,setTypefuncName:"changePlayerLv"});
			
			//绑定sp
			bindList.push({propName:"HP"					,setTypefuncName:"changePlayerHp"});
			bindList.push({propName:"MaxHP"				,setTypefuncName:"changePlayerHp"});
			bindList.push({propName:"MaxHPAdd"				,setTypefuncName:"changePlayerHp"});
			
			bindList.push({propName:"GoldCoin"				,setTypefuncName:"changePlayerGoldCoin"});
			//元宝
			bindList.push({propName:"MagicCoin"		,setTypefuncName:"changePlayerMagicCoin"});
			
			//绑定sp
			bindList.push({propName:"SP"					,setTypefuncName:"changePlayerSp"});
			bindList.push({propName:"MaxSP"				,setTypefuncName:"changePlayerSp"});
			bindList.push({propName:"MaxSPAdd"				,setTypefuncName:"changePlayerSp"});
			
			//vip等级
			bindList.push({propName:"VIPLevel"				,setTypefuncName:"changePlayerVipLevel"});
			
			//SPBonut
			//			bindList.push({propName:"SPCount"				,setTypefuncName:"buySpCount"});
			//
			bindList.push({propName:"SPBonut"				,setTypefuncName:"buySpBonut"});
			//获取元宝数
			//bindList.push({propName:"MagicCoin"				,setTypefuncName:"getMagicCoin"});
			
			bindList.push({propName:"ClotheModel"				,setTypefuncName:"playHeadPhoto"});
			
			bindList.push({propName:"Model"				,setTypefuncName:"playHeadPhoto"});
			//绑定
			var leng	:int	=	bindList.length;
			for( var i:int = 0; i < leng; i ++ )
			{
				_bindManage.BindProp( _role, bindList[i].propName, this, bindList[i].setTypefuncName );
			}
			
			addListener();
		}
		
		private function addListener():void
		{
			addTipsListener( _lvText );
			addTipsListener( _rmbText );
			addTipsListener( _goldText );
			addTipsListener( _spText );
			addTipsListener( _hpText );
			addTipsListener( _spBtn );
			addTipsListener( _faceClip );
			//			addTipsListener( _vipText );
			//			_vip_btn.addEventListener(MouseEvent.CLICK,vipHanler);
			_recharge.addEventListener( MouseEvent.CLICK, onRecharge );
			_spBtn.addEventListener(MouseEvent.CLICK,spBtnHandler); 
			
			_vip_btn.addEventListener(MouseEvent.CLICK,vipWindowHandler);
			_bang_btn.addEventListener(MouseEvent.CLICK,bangWindowHandler);
			_vip_1.addEventListener(MouseEvent.CLICK,vipWindowHandler);
			_bangMobileAfterNumber = BindManage.getInstance().BindProp(
				ModelLocator.getInstance().scene.Role,"DrawedGift",this,"GiveItemAfter");
			bangMobileNumber = BindManage.getInstance().BindProp(
				ModelLocator.getInstance().scene.Role,"AccountIDEx",this,"MobileNumber");
		}
		private function addTipsListener( target : DisplayObject ):void
		{
			target.addEventListener( MouseEvent.MOUSE_OVER, onShowTips );
			target.addEventListener( MouseEvent.MOUSE_OUT, onHideTips ); 
		}
		private function vipWindowHandler(e:MouseEvent):void
		{
			
			//			if(VipLogic.getInstence().ui.created==false)
			//			{
			//				VipLogic.getInstence().ui.showOrHide();
			//			}else
			//			{
			VipLogic.getInstence().ui.showOrHide();
			//			}
			
		}
		/**
		 * 判断是否领取 
		 * @param val
		 * 
		 */		
		public function set GiveItemAfter( val:int):void
		{
			if( !val ){
				_bang_btn.visible = true;
			}else{
				_bang_btn.visible = false;
				MobileCheckUI.getInstance().hide();
				BindManage.getInstance().UnbindProp(ModelLocator.getInstance().scene.Role,
					"DrawedGift",_bangMobileAfterNumber);
			}
			
		}
		
		/**
		 * 手机验证 
		 * 
		 */		
		public function set MobileNumber( val:int ):void
		{
			acceddId = val;
		}
		/**
		 * 绑定
		 * @param event
		 * 
		 */		
		private function bangWindowHandler( event:MouseEvent ) : void
		{
			MobileCheckLogic.getInstance().CheckNumberisAgree( acceddId );
		}
		//vip 按钮
		private function onRecharge( evt : MouseEvent ):void
		{
			VipLogic.getInstence().ui.showOrHide();
			LogSendLogic.getInstance().btnClickNumSend(22116);
			//			_role.recharge();
		}
		//绑定VIPLevel 
		public function set changePlayerVipLevel( value : int ):void
		{
			_vipLevel= value; 
			//			if(_vipLevel>0)
			//			{
			//				_addSpCount =  ClickNum(_vipLevel) ; 
			//			}
			
			//			if( 0 == value )
			//			{
			//				_vipMc.visible = false;
			//			}else
			//			{
			//				_vipMc.visible = true;
			_vipMc.gotoAndStop( value+1 );
			//			}
		}
		
		public function set changePlayerLv( value : int ):void
		{
			_lvText.text = value.toString();
			
			if( value > _level && value != 1 )
			{//等级提升，需要显示特效
				_level = value;
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_TASK_CAN_ACCEPT);
				if(!WalkManager.getInstance().walkLogic.isFight)
				{
					_model.dispatchEvent( new RoleEffectEvent( RoleEffectEvent.LEVEL_UP ) );
					_model.dispatchEvent(new SoundEvent(SoundEvent.LEVEL_UP));
				}
				else
				{
					isLevelUp	=	true;
				}
			}
			if(value>=_roleLevel)
			{
				_spBtn.visible = true ;
			}else
			{
				_spBtn.visible =false ; 
			}
		}
		
		public function get isLevelUp():Boolean
		{
			return isWaitLevelUp;
		}
		
		public function set isLevelUp(val:Boolean):void
		{
			isWaitLevelUp	=	val;
		}
		
		public function set changePlayerHp( value : int ):void
		{
			if( _role.getPlayerHP() <= 0 )
			{
				_hpLine.width =	0;
			}
			else
			{
				_hpLine.width =	_hpLineWidth * Math.min( 1, ( _role.getPlayerMaxHP() / _role.getPlayerMaxHP() ) );
			}
		}
		// 做一个判断不要溢出去 当 现在有的活力大于 最大活力的时候 就会溢出
		public function set changePlayerSp( value : int ):void
		{
			if( _role.getPlayerSP() <= 0 )
			{
				_spLine.width =	0;
			}
			else if ( _role.getPlayerSP()>_role.getPlayerMaxSP())
			{
				_spLine.width =	_spLineWidth * Math.min( 1, 1 );
			}else 
			{
				_spLine.width =	_spLineWidth * Math.min( 1, ( _role.getPlayerSP() / _role.getPlayerMaxSP() ) );
			}
			if( _spText.text != "")
			{
				_spText.text = _role.getPlayerSP() + "/" +  _role.getPlayerMaxSP();
			}
		}
		
		public function set changePlayerGoldCoin( value : int ):void
		{
			if( value < 10000 )
			{
				_goldText.htmlText = value.toString();
			}else
			{
				_goldText.htmlText = Math.floor( value / 10000 ) + StaticIni.getSwfLangStr("UIString_1000629");;
			}
		}
		
		public function set changePlayerMagicCoin( value : int ):void
		{
			_rmbText.text = value.toString();
		}
		public function set fastTrackVisible(val:Boolean):void
		{
			fastTrack.visible	=	val;
			
			//			_ui.powerPlace.visible = _ui.goldPlace.visible = val;
		}
		private function onShowTips( evt : MouseEvent ):void
		{
			var tip : String = "";
			
			switch( evt.target )
			{
				case _lvText:
				{
					tip = StaticIni.getSwfLangStr("Common_Level");
					break;
				}
				case _rmbText:
				{
					tip = StaticIni.getSwfLangStr("MagicCoin") + ":" + _rmbText.text;
					break;
				}
				case _goldText:
				{
					tip = StaticIni.getSwfLangStr("GoldCoin") + ":" + _role.getGoldCoin();
					break;
				}
				case _hpText:
				{
					tip = StaticIni.getSwfLangStr("MaxHPAdd");//生命
					_hpText.text = _role.getPlayerMaxHP() + "/" +  _role.getPlayerMaxHP();//getPlayerHP
					break;
				}
				case _spText:
				{
					tip = StaticIni.getSwfLangStr("UIString_1000665");//体力(每半小时恢复5点)
					_spText.text = _role.getPlayerSP() + "/" +  _role.getPlayerMaxSP();
					break;
				}
				case _vipText:
				{
					tip = "vip1";
					break;
				}
					//添加 SP 的加成按钮
				case _spBtn:
				{
					//VIPTips_10	
					
					if(_model.scene.roleLevel<_roleLevel)
					{
						tip = StaticIni.getSwfLangStr("VIPTips_12");
					}else if (_spCount<_addSpCount)
					{
						tip = StaticIni.getSwfLangStrVar("VIPTips_10",[CostMoneyValue(_spCount+1),int(_addSpCount-_spCount)]);
					}else if (_spCount>=_addSpCount )
					{
						var maxBuyVIPLevel:int = int(StaticIni.getIniVar("SP_Buy","MaxVipLevel"))
						if(_vipLevel>=maxBuyVIPLevel)
						{
							
							tip = StaticIni.getSwfLangStr("VIPTips_8");
						}
						else
						{
							tip = StaticIni.getSwfLangStrVar("VIPTips_15",[int(StaticIni.getIniVar("VipCount",_vipLevel.toString()))]);
						}
					}
					
					break ; 
				}
				case _faceClip:
				{
					getFaceTips();
					break;
				}
			}
			TipsMainLogic.instance.showString( tip );
		}
		private function getFaceTips():void
		{
			if(_rank==-1)
			{
				requestForRank();
			}
			else
			{
				showheadtips();
				requestForRank();
			}
		}
		private function onHideTips( evt : MouseEvent ):void
		{
			TipsMainLogic.instance.hide();
			
			_hpText.text = "";
			_spText.text = "";	
		}
		public function addbuySp():void
		{
			spBtnHandler();
		}
		public function spBtnHandler(e:MouseEvent = null):void
		{
			//			SitAlerLogic.getIntatic().Func(buySp);
			if(_model.scene.roleLevel>=_roleLevel)
			{
				if(_spCount<_addSpCount)
				{
//					if(_role.getPlayerSP()>=_role.getPlayerMaxSP()&&_role.getPlayerSP()!=0)
//					{
//						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("VIPTips_9")); 
//					}
//					else 
					if(SitAlerUI.getInstance().created == false)
					{
						SitAlerLogic.getIntatic().setUIinfo("VIPTips_11",[CostMoneyValue(_spCount+1)],buySp,_spBtn);
					}
				}
			}
			if(e)
			{
				onShowTips(e);
			}
			LogSendLogic.getInstance().btnClickNumSend(22801);
		}
		//购买 SP
		private function buySp():void
		{
			if(_addSpCount==-1)return ; 
			
			if(_spCount<_addSpCount)
			{
				
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_BUY_SP);
				
				
			}else 
			{
				NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("VIPTips_8"));
			}
			
		}
		//当前购买的次数
		public function set buySpCount(value:int):void
		{
			_spCount=value ; 
		}
		/**
		 *count=1,2,6,21,46
		 *money=10,30,60,120,300 
		 * @param num
		 * @return 
		 * 
		 */		
		private function CostMoneyValue(num:int):int
		{
			var strCount:String= _strObj.count;
			var strMoney:String = _strObj.money;
			//			
			var arrCount:Array = strCount.split(",");
			var arrMoney:Array = strMoney.split(",");
			if(arrCount[0]==num)
			{
				return int(arrMoney[0]) ; 
			}else if (arrCount[0]<num&&num<=arrCount[1])
			{
				
				return int(arrMoney[1]) ; 
			}else if (arrCount[1]<num&&num<=arrCount[2])
			{
				
				return int(arrMoney[2]) ; 
			}else if (arrCount[2]<num&&num<=arrCount[3])
			{
				
				return int(arrMoney[3]) ; 
			}else if (arrCount[3]<num)
			{
				return int(arrMoney[4]) ; 
			}
			return -1 ;  
		}
		//		1.	VIP1—VIP3每天可购买1次体力
		//		2.	VIP4—VIP5每天可购买6次体力
		//		3.	VIP6—VIP7每天可购买21次体力
		//		4.	VIP8—VIP10每天可购买46次体力********vipLevel=1,4,6,8
		
		private function ClickNum(level:int):int
		{
			var strVipLevel:String= _strObj.vipLevel;
			var strCount:String= _strObj.count;
			var arrVipLevel:Array = strVipLevel.split(",");
			var arrCount:Array = strCount.split(",");
			if(arrVipLevel[0]<=level&&level<arrVipLevel[1])
			{
				return int(arrCount[0]) ; 
			}else if (arrVipLevel[1]<=level&&level<arrVipLevel[2])
			{
				
				return int(arrCount[2]) ; 
			}else if (arrVipLevel[2]<=level&&level<arrVipLevel[3])
			{
				
				return int(arrCount[3]) ; 
			}else if (arrVipLevel[3]<=level)
			{
				return int(arrCount[4]) ; 
			}
			return -1 ; 
		}
		// 超出SP 最大的值
		public function set buySpBonut(value:int):void
		{
			_beyondSPMax= value ; 
		}
		//获取元宝
		public function set getMagicCoin(value:int):void
		{
			_magicCoin = value;
		}
		/**
		 *  改变玩家头像
		 */		
		public function set playHeadPhoto(val:int):void
		{
			_faceClip.source = ResManager.newResClass( ResourcesPath.getHeadPic( _role.ModelId, _role.clotheModel ));
			// ResourcesPath.getHeadPic( _role.ModelId, _role.clotheModel);
		}
		
	}
}